//
//  DocumentVC.swift
//  Auscultation
//
//  Created by zwb on 2021/3/30.
//

import UIKit

class DocumentVC: BaseVC {
    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var topContraint: NSLayoutConstraint!
    
    var pageNum: Int = 1
    var pageSize: Int = 10
    var studyDataArray: [HomeCourseModel?] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        self.topContraint.constant = UIDevice.lgf_NavBarH()
        self.navTitle = "文档"
        table.register(UINib.init(nibName: "RecomcCourseCell", bundle: Bundle.main), forCellReuseIdentifier: "RecomcCourseCell")
        table.mj_header = MJRefreshNormalHeader.init(refreshingBlock: { [weak self] in
            self?.pageNum = 1
            self?.loadDocumentData()

        })
        
        table.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {
            [weak self] in
            self?.pageNum += 1
            self?.loadDocumentData()

        })

    }


    // MARK: - UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RecomcCourseCell") as! RecomcCourseCell
//        guard let model = courseDataArray[indexPath.row] else { return cell }
//        cell.model = model
        return cell

    }
    
    
    
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 95
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}

extension DocumentVC{

    func loadDocumentData() {
        
        let paramet  = [
            "pageNo" : pageNum.description,
            "pageSize" : pageSize.description
        ]
        EWERequest.lgf("course", paramet, .get) {  (data) in
            guard let result = (data["result"] as? [Any]?) else { return  }
            if self.pageNum == 1 {
                self.studyDataArray.removeAll()
                self.table.mj_header?.endRefreshing()
            }
            let arr = Array<HomeCourseModel>.deserialize(from: result)
            if arr?.count == 0 {
                self.table.mj_footer?.endRefreshingWithNoMoreData()
            }else{
                self.table.mj_footer?.endRefreshing()
            }
            self.studyDataArray += arr ?? []
            self.table.reloadData()
        } _: {  (error) in
            SVProgressHUD.showError(error)
        }

    }
    
}
    
